package com.ruoyi.merchant.utils;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.merchant.domain.Merchant;
import com.ruoyi.merchant.mapper.MerchantMapper;

/**
 * 商家安全工具类
 * 用于获取当前登录商家的信息
 * 
 * @author ruoyi
 */
public class MerchantSecurityUtils {

    /**
     * 获取MerchantMapper的Bean（通过SpringUtils动态获取）
     */
    private static MerchantMapper getMerchantMapper() {
        return SpringUtils.getBean(MerchantMapper.class);
    }

    /**
     * 获取当前登录商家ID
     * 
     * @return 商家ID
     * @throws ServiceException 如果未登录或商家不存在
     */
    public static Long getMerchantId() {
        LoginUser loginUser = getLoginUser();
        
        // 通过user_id查询商家信息
        Merchant merchant = getMerchantMapper().selectMerchantByUserId(loginUser.getUserId());
        if (merchant == null) {
            throw new ServiceException("商家信息不存在");
        }

        return merchant.getMerchantId();
    }

    /**
     * 获取当前登录商家的完整信息
     * 
     * @return 商家信息
     * @throws ServiceException 如果未登录或商家不存在
     */
    public static Merchant getCurrentMerchant() {
        LoginUser loginUser = getLoginUser();
        
        Merchant merchant = getMerchantMapper().selectMerchantByUserId(loginUser.getUserId());
        if (merchant == null) {
            throw new ServiceException("商家信息不存在");
        }

        return merchant;
    }

    /**
     * 获取当前登录用户的UserId
     * 
     * @return 用户ID
     * @throws ServiceException 如果未登录
     */
    public static Long getUserId() {
        LoginUser loginUser = getLoginUser();
        return loginUser.getUserId();
    }

    /**
     * 获取当前登录用户信息
     * 
     * @return LoginUser
     * @throws ServiceException 如果未登录
     */
    public static LoginUser getLoginUser() {
        try {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (loginUser == null) {
                throw new ServiceException("请先登录", 401);
            }
            return loginUser;
        } catch (Exception e) {
            throw new ServiceException("请先登录", 401);
        }
    }

    /**
     * 获取当前登录商家的手机号
     * 
     * @return 手机号
     */
    public static String getContactPhone() {
        Merchant merchant = getCurrentMerchant();
        return merchant.getContactPhone();
    }

    /**
     * 获取当前登录商家的名称
     * 
     * @return 商家名称
     */
    public static String getMerchantName() {
        Merchant merchant = getCurrentMerchant();
        return merchant.getMerchantName();
    }

    /**
     * 检查当前用户是否为商家
     * 
     * @return true-是商家，false-不是商家
     */
  /*  public static boolean isMerchant() {
        try {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (loginUser == null || loginUser.getUser() == null) {
                return false;
            }
            
            // 检查用户类型
            String userType = loginUser.getUser().getUserType();
            if (!"merchant".equals(userType)) {
                return false;
            }
            
            // 检查是否存在商家记录
            Merchant merchant = getMerchantMapper().selectMerchantByUserId(loginUser.getUserId());
            return merchant != null;
            
        } catch (Exception e) {
            return false;
        }
    }
*/
    /**
     * 检查当前商家是否已审核通过
     * 
     * @return true-已审核通过，false-未通过
     */
    public static boolean isApproved() {
        Merchant merchant = getCurrentMerchant();
        return "APPROVED".equals(merchant.getExternField1());
    }

    /**
     * 检查当前商家是否可以营业
     * 
     * @return true-可以营业，false-不可以营业
     */
    public static boolean canOperate() {
        Merchant merchant = getCurrentMerchant();
        return "APPROVED".equals(merchant.getExternField1()) 
                && merchant.getStatus() == 0;
    }

    /**
     * 获取当前商家的审核状态
     * 
     * @return 审核状态：PENDING_INFO/UNDER_REVIEW/APPROVED/REJECTED
     */
    public static String getAuditStatus() {
        Merchant merchant = getCurrentMerchant();
        return merchant.getExternField1() != null ? merchant.getExternField1() : "PENDING_INFO";
    }

    /**
     * 校验当前商家是否有操作权限（必须是审核通过的商家）
     * 
     * @throws ServiceException 如果没有权限
     */
    public static void checkOperatePermission() {
        if (!isApproved()) {
            throw new ServiceException("商家未通过审核，无法操作");
        }
    }

    /**
     * 校验当前商家是否可以营业
     * 
     * @throws ServiceException 如果不可以营业
     */
    public static void checkCanOperate() {
        Merchant merchant = getCurrentMerchant();
        
        if (merchant.getStatus() == 2) {
            throw new ServiceException("商家已被封禁");
        }
        
        if (!"APPROVED".equals(merchant.getExternField1())) {
            throw new ServiceException("商家未通过审核");
        }
        
        if (merchant.getStatus() == 1) {
            throw new ServiceException("商家当前处于休息状态");
        }
    }
}
